package com.epoint.mobileoa.tools;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.PrintWriter;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.webkit.MimeTypeMap;



public class IOHelp {

	//Bitmap转为二进制
	public static byte[] Bitmap2Bytes(Bitmap bm) {     
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray(); 
	}

	//对象转为二进制
	public static byte[] obj2bytes(Object obj) throws IOException{
		ByteArrayOutputStream fos = new ByteArrayOutputStream();
		ObjectOutputStream oos;
		oos = new ObjectOutputStream(fos);
		oos.writeObject(obj);
		return fos.toByteArray();
	}

	//二进制转为对象
	public static Object bytes2obj(byte[] b) throws OptionalDataException, ClassNotFoundException, IOException{
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		ObjectInputStream ois = new ObjectInputStream(bais);
		return ois.readObject();
	}

	//向一个文本中续写数据
	public static void TextFileContinueWrite(String path,String log){
		try{
			PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(path, true)));
			pw.write("\r\n");
			pw.write(log);
			pw.close();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	//流转为二进制
	public static byte[] InputStreamToByte(InputStream is){
		try{
			ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
			int ch;
			while ((ch = is.read()) != -1) {
				bytestream.write(ch);
			}
			byte imgdata[] = bytestream.toByteArray();
			bytestream.close();
			return imgdata;
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static byte[] File2Byte(File f)
	{
		try{
			InputStream is = new FileInputStream(f);
			return InputStreamToByte(is);
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	//创建文件夹
	public static void FoldCreate(String path){
		File f = new File(path);
		if(!f.exists()){
			f.mkdirs();
		}
	}

	public static void deleteFile(File file){ 
		if(file.exists()){                    //判断文件是否存在
			if(file.isFile()){                    //判断是否是文件
				file.delete();                       //delete()方法 你应该知道 是删除的意思;
			}else if(file.isDirectory()){              //否则如果它是一个目录
				File files[] = file.listFiles();               //声明目录下所有的文件 files[];
				for(int i=0;i<files.length;i++){            //遍历目录下所有的文件
					deleteFile(files[i]);             //把每个文件 用这个方法进行迭代
				} 
			} 
			file.delete(); 
		}
	}

	public static void doOpenFile(Context con,String attachfilepath){
		if(!attachfilepath.contains(".")){
			ToastHelp.showToast(con, "未知文件类型");
			return;
		}

		try{
			if(attachfilepath.contains(".doc")||attachfilepath.contains(".docx")){
				Intent aIntent = new Intent(Intent.ACTION_VIEW);
				aIntent.setClassName("com.yozo.office", "emo.main.MainAppProxy");
				aIntent.setData(Uri.fromFile(new File(attachfilepath)));

				//reOnly为false则是可编辑，true则为只读
				aIntent.putExtra("readOnly", false);
				con.startActivity(aIntent);
				return;
			}
		}catch(Exception e){
			Log.i("Epoint", "没有安装YOZO Office");
		}

		//新建

		/**
		 * fileTypeAction为“CREATE_SS”表示新建xls文档
		 * fileTypeAction为“CREATE_WP”表示新建doc文档
		 * fileTypeAction为“CREATE_PG”表示新建ppt文档
		 *//*
		Intent aIntent = new Intent(fileTypeAction);
		aIntent.setClassName("com.yozo.office", "emo.main.MainAppProxy");
		con.startActivity(aIntent);*/



		String suffix = attachfilepath.substring(attachfilepath.lastIndexOf("."),attachfilepath.length()).toLowerCase();
		Intent intent = new Intent(Intent.ACTION_VIEW);
		String param = "file://" + attachfilepath;
		Uri uri = Uri.parse(param);
		String type = null;
		type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
				MimeTypeMap.getFileExtensionFromUrl(attachfilepath));

		if (type == null) {
			String tmp = attachfilepath.toLowerCase();
			if (tmp.endsWith("mp3") || tmp.endsWith("wav") || tmp.endsWith("wma")) {
				type = "audio/*";
			} else if (tmp.endsWith("apk")) {
				type = "application/vnd.android.package-archive";
			}
		}
		if(type != null&&!suffix.equals(".rar")){
			intent.setDataAndType(uri, type);
			try {
				((Activity) con).startActivityForResult(intent, 1);
			} catch (ActivityNotFoundException e){
				ToastHelp.showToastCommon(con, "文件不能打开");
			}
		} 
		else {
			try{
				Intent intent1 = null;

				if(suffix.equals(".jpg")||suffix.equals(".png")||suffix.equals(".gif")||suffix.equals(".bmp")){
					intent1 = Service.getImageFileIntent(attachfilepath);
				}
				else if(suffix.equals(".pdf")){
					intent1  = Service.getPdfFileIntent(attachfilepath);
				}
				else if(suffix.equals(".doc")||suffix.equals(".docx")){
					intent1  = Service.getWordFileIntent(attachfilepath);
				}
				else if(suffix.equals(".xls")||suffix.equals(".xlsx")){
					intent1  = Service.getExcelFileIntent(attachfilepath,con);
				}
				else if(suffix.equals(".mp3")||suffix.equals(".wma")||suffix.equals(".mp4")||suffix.equals(".wav")){
					intent1  = Service.getAudioFileIntent(attachfilepath);
				}
				else if(suffix.equals(".txt")){
					intent1  = Service.getTextFileIntent(attachfilepath, false);
				}
				else if(suffix.equals(".html")){
					intent1  = Service.getHtmlFileIntent(attachfilepath);
				}
				else if(suffix.equals(".ppt")||suffix.equals(".pptx")){
					intent1  = Service.getPptFileIntent(attachfilepath);
				}
				else if(suffix.equals(".rar")||suffix.equals(".zip")){
					intent1  = Service.getRARFileIntent(attachfilepath);
				}
				con.startActivity(intent1);
			}catch(Exception e){
				ToastHelp.showToastCommon(con, "没有适合的程序打开此文件");
			}
		}
	}

	/**
	 * 拷贝文件
	 * @param con
	 * @param assfilename
	 * @param destpathname
	 * @throws IOException 
	 */
	public static void copyFile(String assfilename,String destpathname) throws IOException{
		FileInputStream fis = new FileInputStream(assfilename);
		FileOutputStream fos = new FileOutputStream(destpathname);
		byte data[] = new byte[1024];
		int count;
		while ((count = fis.read(data)) != -1) {
			fos.write(data, 0, count);
		}
		fos.flush();
		fos.close();
		fis.close();
	}

	//读取文件中字符串
	public static String readtxt(String path){
		try {
			BufferedReader br= new BufferedReader(new FileReader(path));
			String str="";
			String r=br.readLine();
			while(r!=null){
				str+=r;
				r=br.readLine();
			}
			return str;
		} catch (Exception e) {
			return "";
		}
	}

	public static void writeText2Path(String path,String msg){
		try{
			FileOutputStream fos = new FileOutputStream(path);
			fos.write(msg.getBytes());
			fos.flush();
			fos.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public static boolean saveFile(byte[] b,String filepath)
	{
		try{ 
			FileOutputStream out = new FileOutputStream(filepath);
			out.write(b);
			out.close();
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
}
